home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / Temperature / MainMenu.cp < prev    next >
Encoding:
Text File  |  1998-09-06  |  5.0 KB  |  252 lines  |  [TEXT/CWIE]

  1. /* MainMenu.cp */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <LowMem.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <ToolUtils.h>
  13.  
  14. #include "Globals.h"
  15. #include "ResourceDefs.h"
  16. #include "Miscellany.h"
  17. #include "AMApp.h"
  18. #include "AMDoc.h"
  19. #include "AMEngine.h"
  20. #include "AMWindow.h"
  21. #include "MainMenu.h"
  22.  
  23.  
  24. /*----------*/
  25. static long        GetCommandFromMenu (long        menuChoice);
  26. static void        DoApple            (short            itemNr);
  27. static void        Enable            (short            itemNr,
  28.                                  Boolean        enabled);
  29. static void        EnableTitle        (MenuHandle        menu,
  30.                                  Boolean        enabled);
  31.  
  32. /*----------*/
  33. void    InitTitles (void)
  34. {
  35. } /*InitTitles*/
  36.  
  37. /*----------*/
  38. void    LoadMenus (void)
  39. {
  40.     AppleMenu    = GetMenu (MENU_Apple);
  41.     FailNilResource ((Handle)AppleMenu);
  42.     AppendResMenu (AppleMenu, 'DRVR');
  43.     FileMenu    = GetMenu (MENU_File);
  44.     EditMenu    = GetMenu (MENU_Edit);
  45.  
  46.     InsertMenu (AppleMenu, 0);
  47.     InsertMenu (FileMenu, 0);
  48.     InsertMenu (EditMenu, 0);
  49.  
  50.     DrawMenuBar ();
  51. } /*LoadMenus*/
  52.  
  53. //----------
  54. long    GetCommandFromMenu (
  55.     long        menuChoice)
  56. {
  57.     long        commandID = 0;
  58.  
  59.     switch (menuChoice) {
  60.         case cAppleAbout:
  61.                 commandID = cmdInvokeAbout;
  62.             break;
  63.         case cFileNew:
  64.                 commandID = cmdNew;
  65.             break;
  66.         case cFileOpen:
  67.                 commandID = cmdOpen;
  68.             break;
  69.         case cFileClose:
  70.                 commandID = cmdClose;
  71.             break;
  72.         case cFileSave:
  73.                 commandID = cmdSave;
  74.             break;
  75.         case cFileSaveAs:
  76.                 commandID = cmdSaveAs;
  77.             break;
  78.         case cFileRevert:
  79.                 commandID = cmdRevert;
  80.             break;
  81.         case cFilePageSetup:
  82.                 commandID = cmdPageSetup;
  83.             break;
  84.         case cFilePrint:
  85.                 commandID = cmdPrint;
  86.             break;
  87.         case cFileQuit:
  88.                 commandID = cmdQuit;
  89.             break;
  90.         case cEditUndo:
  91.                 commandID = cmdUndo;
  92.             break;
  93.         case cEditCut:
  94.                 commandID = cmdCut;
  95.             break;
  96.         case cEditCopy:
  97.                 commandID = cmdCopy;
  98.             break;
  99.         case cEditPaste:
  100.                 commandID = cmdPaste;
  101.             break;
  102.         case cEditClear:
  103.                 commandID = cmdClear;
  104.             break;
  105.         case cEditSelectAll:
  106.                 commandID = cmdSelectAll;
  107.             break;
  108.         case cEditShowClipboard:
  109.                 commandID = cmdShowClipboard;
  110.             break;
  111.  
  112.         default:
  113.                 commandID = -menuChoice;
  114.     }
  115.  
  116.     return commandID;
  117. }
  118.  
  119. //----------
  120. void    DoApple (
  121.     short        itemNr)
  122. {
  123.     Str255            name;
  124.     short            refNum;
  125.  
  126.     GetMenuItemText (AppleMenu, itemNr, name);
  127.     refNum = OpenDeskAcc (name);
  128. }
  129.  
  130. /*----------*/
  131. void    DoMenu (
  132.     long        menuChoice)
  133. {
  134.     long            commandID;
  135.     AMDoc*            curDoc;
  136.     short            menuID;
  137.     short            itemNr;
  138.  
  139.     commandID = GetCommandFromMenu (menuChoice);
  140.     curDoc = cur->mDoc;
  141.     if (cur->DoCommand (commandID)) {
  142.         // cur window handled it
  143.     } else if ((curDoc != nil)
  144.     && (curDoc->DoCommand (commandID))) {
  145.         // document handled it
  146.     } else if (gApplication->DoCommand (commandID)) {
  147.         // application handled it
  148.     } else {
  149.         menuID = HiWord (menuChoice);
  150.         itemNr = LoWord (menuChoice);
  151.         if (menuID == MENU_Apple) {
  152.             DoApple (itemNr);
  153.         }
  154.     }
  155.  
  156.     HiliteMenu (0);
  157. } /*DoMenu*/
  158.  
  159. /*----------*/
  160. MenuHandle        menu;
  161. Boolean            menuBarChanged;
  162.  
  163. /*----------*/
  164. static void Enable    (short        itemNr,
  165.                      Boolean    enabled)
  166. {
  167.     if (enabled) {
  168.         EnableItem  (menu, itemNr);
  169.     } else {
  170.         DisableItem (menu, itemNr);
  171.     }
  172. } /*Enable*/
  173.  
  174. /*----------*/
  175. static void EnableTitle    (MenuHandle        menu,
  176.                          Boolean        enabled)
  177. {
  178.     if (enabled != ((**menu).enableFlags & 1)) {
  179.         menuBarChanged = true;
  180.     }
  181.     if (enabled) {
  182.         EnableItem  (menu, 0);
  183.     } else {
  184.         DisableItem (menu, 0);
  185.     }
  186. } /*EnableTitle*/
  187.  
  188. /*----------*/
  189. void    UpdateMenus (void)
  190. {
  191.     WindowPeek        frontPeek;
  192.     Boolean            isFront;        /*is there a front window?*/
  193.     Boolean            isCur;            /*is there a current window?*/
  194.     Boolean            isCurDoc;        /*is there a current document?*/
  195.     Boolean            isDirty;        /*is it dirty?*/
  196.     Boolean            hasFile;        /*does it have a file?*/
  197.     Boolean            isSelected;        /*is anything selected?*/
  198.     Boolean            isDesk;            /*is the front window a desk acc?*/
  199.     Boolean            isText;            /*is there a current text field?*/
  200.     Boolean            isScrap;        /*is there any scrap?*/
  201.     menuBarChanged = false;
  202.  
  203.     isFront        = (FrontWindow () != nil);
  204.     isCur        = (curWindow != nil);
  205.     isDirty        = false;
  206.     hasFile        = false;
  207.     isSelected    = false;
  208.     isCurDoc    = (cur->mDoc != nil);
  209.     if (isCurDoc) {
  210.         isDirty        = cur->mDoc->mEngine->IsDirty ();
  211.         hasFile        = cur->mDoc->mEngine->HasFile ();
  212.     }
  213.  
  214.     isDesk = false;
  215.     if (isFront) {
  216.         frontPeek    = (WindowPeek) FrontWindow ();
  217.         isDesk        = (frontPeek->windowKind < 0);
  218.     }
  219.  
  220.     TEHandle    curTE = cur->GetCurTE ();
  221.  
  222.     isText        = (curTE != nil);
  223.     isScrap        = false;
  224.     if (isText) {
  225.         isSelected    = ((**curTE).selStart != (**curTE).selEnd);
  226.         isScrap        = (TEGetScrapLength () > 0);
  227.     }
  228.  
  229.     menu = FileMenu;
  230.     Enable (cFileClose,        isFront);
  231.     Enable (cFileSave,        isDirty);
  232.     Enable (cFileSaveAs,    isCur);
  233.     Enable (cFileRevert,    isDirty);
  234.  
  235.     menu = EditMenu;
  236.     if (isFront) {
  237.         Enable (cEditUndo,        isDesk);
  238.         Enable (cEditCut,        isDesk || isSelected);
  239.         Enable (cEditCopy,        isDesk || isSelected);
  240.         Enable (cEditPaste,        isDesk || isScrap);
  241.         Enable (cEditClear,        isDesk || isSelected);
  242.         Enable (cEditSelectAll,    isText);
  243.  
  244.     }
  245.     EnableTitle (EditMenu,     isFront);
  246.  
  247.  
  248.     if (menuBarChanged) {
  249.         DrawMenuBar ();
  250.     }
  251. } /*UpdateMenus*/
  252.